package runesim.node.model;
import runesim.node.net.ActionSender;
/**
 * Handles player's inventory items.
 * @author Jim
 */
public class InventoryParent {

			Item[] inventoryItems = new Item[28];
			Player player;
			
			public InventoryParent(Player player){
				this.player = player;
				
				for(int i = 0; i < inventoryItems.length; i++){
					inventoryItems[i] = new Item(0, 0, i);
				}
			}
			
			public int freeSlots(){
				int free = 0;
				for(Item i : inventoryItems){
					if(i.getAmount() == 0) free++;
				}
				return free;
			}
			
			public boolean hasFreeSlots(){
				int free = 0;
				for(Item i : inventoryItems){
					if(i.getAmount() == 0) free++;
				}
				return free > 0;
			}
			
			public int nextFreeSlot(){
				for(int i = 0; i < inventoryItems.length; i++){
					if(inventoryItems[i].getAmount() == 0) return i;
				}
				
				return -1;
			}
			
			public Item[] getInventoryItems(){
				return inventoryItems;
			}
			
			public void emptyInventory(){
				for(int i = 0; i < inventoryItems.length; i++){
					inventoryItems[i].setID(0);
					inventoryItems[i].setAmount(0);
				}
				
				player.getActionSender().sendResetInventory();
			}
			
			public void addItem(int id, int amount){
				ActionSender as = player.getActionSender();
				boolean stackable = false; //lookup in item definition here
				
				if(hasFreeSlots()){
					if(stackable){ //Needs only one slot
						int nextFree = nextFreeSlot();
						inventoryItems[nextFree].setID(id);
						inventoryItems[nextFree].setAmount(amount);
						as.sendInventoryItem(id, amount, nextFree);
					} else {
						if(freeSlots() >= amount){
							for(int i = 0; i < amount; i++){
								int nextFree = nextFreeSlot();
								inventoryItems[nextFree].setID(id);
								inventoryItems[nextFree].setAmount(1);
								as.sendInventoryItem(id, 1, nextFree);
							}
						} else {
							runesim.testing.RuneScriptMethods rsm = new runesim.testing.RuneScriptMethods(player);
							rsm.mes("Not enough space in your inventory."); //TODO Find real message
						}
					}
				} else {
					runesim.testing.RuneScriptMethods rsm = new runesim.testing.RuneScriptMethods(player);
					rsm.mes("Not enough space in your inventory."); //TODO Find real message
				}
			}
			
			public int getAmountForSlot(int slot){
				return inventoryItems[slot].getAmount();
			}
			
			public int getIDForSlot(int slot){
				return inventoryItems[slot].getID();
			}
			
			public boolean hasItem(int id){
				for(int i = 0; i < inventoryItems.length; i++){
					if(inventoryItems[i].getID() == id){
						return true;
					}
				}
				
				return false;
			}
			
			public int slotsOfID(int id){
				int count = 0;
				for(int i = 0; i < inventoryItems.length; i++){
					if(inventoryItems[i].getID() == id){
						count++;
					}
				}
				return count;
			}
			
			public Item[] queryForID(int id){
				Item[] tmp = new Item[slotsOfID(id)];
				int index = 0;
				
				for(int i = 0; i < inventoryItems.length; i++){
					if(inventoryItems[i].getID() == id){
						tmp[index++] = inventoryItems[i];
					}
				}
				
				return tmp;
			}
			
			public void removeAllOfItemID(int id){
				int removed = 0;
				for(int i = 0; i < inventoryItems.length; i++){
					if(inventoryItems[i].getID() == id){
						inventoryItems[i].setID(0);
						inventoryItems[i].setAmount(0);
						removed++;
					}
				}
				
				if(removed > 0){ //If something changed
					player.getActionSender().sendResetInventory();
				}
			}
			
			public void swapSlot(int from, int to){
				int oldID = inventoryItems[from].getID();
				int oldAmount = inventoryItems[from].getAmount();
				inventoryItems[from].setID(inventoryItems[to].getID());
				inventoryItems[from].setAmount(inventoryItems[to].getAmount());
				inventoryItems[to].setID(oldID);
				inventoryItems[to].setAmount(oldAmount);
				player.getActionSender().sendResetInventory();
			}
			
			public void removeItem(int slot){
				inventoryItems[slot].setID(0);
				inventoryItems[slot].setAmount(0);
				player.getActionSender().sendResetInventory();
			}
			
			/**
			 * Might not work as planned yet...
			 * @param id
			 * @param amount
			 */
			public void removeItem(int id, int amount){
				for(int i = 0; i < inventoryItems.length; i++){
					removeItem(id, i, amount, true);
				}
				player.getActionSender().sendResetInventory();
			}
			
			public void removeItem(int id, int amount, int slot){
				removeItem(id, slot, amount, false);
			}
			
			void removeItem(int id, int slot, int amount, boolean multipleMode){
				Item i = inventoryItems[slot];
				
				if(i.getID() == id){
					if(i.getAmount() > amount){
						i.setAmount(i.getAmount() - amount);
					} else {
						i.setID(0);
						i.setAmount(0);
					}
					inventoryItems[slot] = i;
					if(!multipleMode) player.getActionSender().sendResetInventory();
				}
			}
}